package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.DryTime;
import model.Subtreatment;
import model.Treatment;
import service.Service;

public class CreateTreatment extends JFrame {
	private JTextField textName;
	private JTextField textMinDry;
	private JTextField textOpDry;
	private JTextField textMaxDry;
	private JButton btnRemove, btnAdd, btnMoveUp, btnMoveDown,
			btnCreateNewTreat, btnDelete, btnDeleteSubtreat;
	private JList listAvailableSubTreat, listTreat, listPickedSubTreat;
	private JScrollPane scrollTreat, scrollPickedSubTreat,
			scrollAvailableSubTreat;
	private JLabel lblNameWarning, lblSubtreatWarning;

	private ListController listController = new ListController();
	private final Controller controller = new Controller();

	private Service service = Service.getInstance();

	public CreateTreatment() {

		getContentPane().setLayout(null);
		this.setSize(879, 595);

		JLabel lblName = new JLabel("Navn");
		lblName.setBounds(11, 11, 46, 14);
		getContentPane().add(lblName);

		textName = new JTextField();
		textName.setBounds(11, 36, 86, 20);
		getContentPane().add(textName);
		textName.setColumns(10);

		btnCreateNewTreat = new JButton("Opret ny behandling");
		btnCreateNewTreat.setBounds(107, 35, 165, 23);
		getContentPane().add(btnCreateNewTreat);
		btnCreateNewTreat.addActionListener(controller);

		JLabel lblMinDry = new JLabel("Minimum t\u00F8rretid");
		lblMinDry.setBounds(374, 114, 86, 14);
		getContentPane().add(lblMinDry);

		textMinDry = new JTextField();
		textMinDry.setBounds(374, 139, 86, 20);
		getContentPane().add(textMinDry);
		textMinDry.setColumns(10);
		textMinDry.setEditable(false);

		JLabel lblOpDry = new JLabel("Optimal t\u00F8rretid");
		lblOpDry.setBounds(374, 170, 97, 14);
		getContentPane().add(lblOpDry);

		textOpDry = new JTextField();
		textOpDry.setBounds(374, 195, 86, 20);
		getContentPane().add(textOpDry);
		textOpDry.setColumns(10);
		textOpDry.setEditable(false);

		JLabel lblMaxDry = new JLabel("Maksimal t\u00F8rretid");
		lblMaxDry.setBounds(374, 226, 86, 14);
		getContentPane().add(lblMaxDry);

		textMaxDry = new JTextField();
		textMaxDry.setBounds(374, 248, 86, 20);
		getContentPane().add(textMaxDry);
		textMaxDry.setColumns(10);
		textMaxDry.setEditable(false);

		btnRemove = new JButton("Fjern");
		btnRemove.setBounds(374, 279, 120, 23);
		getContentPane().add(btnRemove);
		btnRemove.addActionListener(controller);

		btnAdd = new JButton("Tilf\u00F8j");
		btnAdd.setBounds(514, 316, 115, 23);
		getContentPane().add(btnAdd);
		btnAdd.addActionListener(controller);

		btnMoveUp = new JButton("Flyt op");
		btnMoveUp.setBounds(219, 316, 86, 23);
		getContentPane().add(btnMoveUp);
		btnMoveUp.addActionListener(controller);

		btnMoveDown = new JButton("Flyt ned");
		btnMoveDown.setBounds(219, 350, 86, 23);
		getContentPane().add(btnMoveDown);
		btnMoveDown.addActionListener(controller);

		scrollTreat = new JScrollPane();
		scrollTreat.setBounds(11, 113, 181, 192);
		getContentPane().add(scrollTreat);

		listTreat = new JList();
		scrollTreat.setViewportView(listTreat);
		listTreat.setListData(service.getTreatments().toArray());
		listTreat.addListSelectionListener(listController);

		scrollPickedSubTreat = new JScrollPane();
		scrollPickedSubTreat.setBounds(219, 113, 145, 192);
		getContentPane().add(scrollPickedSubTreat);

		listPickedSubTreat = new JList();
		scrollPickedSubTreat.setViewportView(listPickedSubTreat);

		listPickedSubTreat
				.addListSelectionListener(new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						textMinDry.setText("");
						textMaxDry.setText("");
						textOpDry.setText("");
						if (!listPickedSubTreat.isSelectionEmpty()) {
							if (listPickedSubTreat.getSelectedValue() instanceof DryTime) {
								DryTime subtreatment = (DryTime) listPickedSubTreat
										.getSelectedValue();
								textMinDry.setText(""
										+ subtreatment.getMinDryTime());
								textMaxDry.setText(""
										+ subtreatment.getMaxDryTime());
								textOpDry.setText(""
										+ subtreatment.getPerDryTime());
							}
						}

					}
				});

		scrollAvailableSubTreat = new JScrollPane();
		scrollAvailableSubTreat.setBounds(504, 110, 145, 195);
		getContentPane().add(scrollAvailableSubTreat);

		listAvailableSubTreat = new JList();
		scrollAvailableSubTreat.setViewportView(listAvailableSubTreat);
		listAvailableSubTreat.setListData(service.getSubtreatments().toArray());

		lblNameWarning = new JLabel("");
		lblNameWarning.setBounds(284, 38, 236, 16);
		getContentPane().add(lblNameWarning);

		lblSubtreatWarning = new JLabel("");
		lblSubtreatWarning.setBounds(671, 170, 165, 53);
		getContentPane().add(lblSubtreatWarning);

		btnDelete = new JButton("Slet");
		btnDelete.setBounds(11, 313, 117, 29);
		getContentPane().add(btnDelete);

		btnDeleteSubtreat = new JButton("Slet");
		btnDeleteSubtreat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Subtreatment subtreatment = (Subtreatment) listAvailableSubTreat
						.getSelectedValue();
				service.deleteSubtreatment(subtreatment);
				listAvailableSubTreat.setListData(service.getSubtreatments()
						.toArray());
			}

		});
		btnDeleteSubtreat.setBounds(514, 347, 117, 29);
		getContentPane().add(btnDeleteSubtreat);

	}

	private class ListController implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent le) {

			if (!listTreat.isSelectionEmpty()) {
				Treatment treatment = (Treatment) listTreat.getSelectedValue();
				listPickedSubTreat.setListData(treatment.getSubtreatments()
						.toArray());
			}
		}
	}

	private class Controller implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == btnCreateNewTreat) {
				String name = "";
				Subtreatment subtreatment = null;

				if (textName.getText().length() > 0) {
					name = textName.getText();
					subtreatment = (Subtreatment) listAvailableSubTreat
							.getSelectedValue();
					service.storeTreatment(service.createTreatment(name,
							subtreatment));
					lblNameWarning.setText("");
				} else {
					lblNameWarning.setText("Tilf\u00F8j navn!");

				}

				listTreat.setListData(service.getTreatments().toArray());
				textName.setText("");

			} else if (e.getSource() == btnRemove) {
				Treatment treatment = (Treatment) listTreat.getSelectedValue();
				Subtreatment subtreatment = (Subtreatment) listPickedSubTreat
						.getSelectedValue();
				treatment.removeSubtreatment(subtreatment);
				listPickedSubTreat.setListData(treatment.getSubtreatments()
						.toArray());

			} else if (e.getSource() == btnAdd) {
				Treatment treatment = (Treatment) listTreat.getSelectedValue();
				Subtreatment subtreatment = (Subtreatment) listAvailableSubTreat
						.getSelectedValue();

				if (listTreat.getSelectedValue() == null) {

					lblSubtreatWarning.setText("V\u00E6lg behandling!");
				} else if (listTreat.getSelectedValue() != null) {
					if (listAvailableSubTreat.getSelectedValue() != null) {
						treatment.addSubtreatment(subtreatment);
						listPickedSubTreat.setListData(treatment
								.getSubtreatments().toArray());
					} else if (listAvailableSubTreat.getSelectedValue() == null) {
						lblSubtreatWarning.setText("V\u00E6lg delbehandling");
					}
				}
			} else if (e.getSource() == btnMoveUp) {
				Treatment treatment = (Treatment) listTreat.getSelectedValue();
				Subtreatment subtreatment = (Subtreatment) listPickedSubTreat
						.getSelectedValue();
				treatment.moveUp(subtreatment);

				listPickedSubTreat.setListData(treatment.getSubtreatments()
						.toArray());

				listPickedSubTreat.setSelectedIndex(treatment
						.getSubtreatments().indexOf(subtreatment));

			} else if (e.getSource() == btnMoveDown) {
				Treatment treatment = (Treatment) listTreat.getSelectedValue();
				Subtreatment subtreatment = (Subtreatment) listPickedSubTreat
						.getSelectedValue();
				treatment.moveDown(subtreatment);

				listPickedSubTreat.setListData(treatment.getSubtreatments()
						.toArray());

				listPickedSubTreat.setSelectedIndex(treatment
						.getSubtreatments().indexOf(subtreatment));

			} else if (e.getSource() == btnDelete) {
				if (listTreat.getSelectedValue() != null) {
					Treatment treatment = (Treatment) listTreat
							.getSelectedValue();
					treatment.getSubtreatments();
					listPickedSubTreat.removeAll();
					service.deleteTreatment(treatment);

				} else {

				}

				listTreat.setListData(service.getTreatments().toArray());
				// listPickedSubTreat.setListData(service.get)

			}

		}
	}
}
